$(function () {

    function getCategoryList() {
        $.get('/admin/category/list', function (res) {
            let html = ''
            res.data.forEach(index => {
                html += `<option value='${index.id}'>${index.name}</option>`
            })
            $('#category').html(html)
        })
    }
    function render() {
        layui.laydate.render({
            elem: '#articleDate',//指定元素
            theme: '#393D49'
        })
    }
    function tin() {
        tinymce.init({
            selector: '#articleContent',
            language: 'zh_CN'

        })
    }
    render()
    getCategoryList()
    tin()
    $("#inputCover").change(function () {
        let files = this.files[0]
        $('.article_cover').attr('src', URL.createObjectURL(files))
    })
    function saveArticle(type) {
        const form = $('#form')[0]
        const fd = new FormData(form)
        const content = tinyMCE.editors['articleContent'].getContent();
        fd.append('content', content)
        if (type === '已发布') {
            fd.append('state', "已发布")
        } else {
            fd.append('state', '')
        }
        $.ajax({
            url: '/admin/article/publish',
            data: fd,
            type: 'post',
            contentType: false,
            processData: false,
            success(res) {
                console.log(res)
                if (res.code === 200) {
                    location.href = 'article_list.html'
                } else {
                    layui.layer.msg(res.msg);
                    return
                }
            }
        })

    }
    window.saveArticle = saveArticle
})
